﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ActiveStory.IO.XmlElementNames;

namespace ActiveStory.IO
{
    public class XmlActiveStoryPageWriter : IActiveStoryPageWriter
    {
        #region IActiveStoryPageWriter Members
        /// <summary>
        /// Writes the page file in xml format.
        /// </summary>
        /// <param name="pageFilename">The absolute path filename to save the page file as.</param>
        /// <param name="pageName">The name of the page.</param>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="thumbnailFileLocation">The relative path of the thumbnail file.</param>
        /// <param name="xamlFileLocation">The relative path of the Xaml file.</param>
        /// <param name="buttonMapFileLocation">The relative path of the buttonToPageMap file.</param>
        /// <param name="buttonCounter">The last value of the button counter.</param>
        public void SaveFile(string pageFilename, string pageName, int pageNumber, string thumbnailFileLocation, string xamlFileLocation, string buttonMapFileLocation, int buttonCounter)
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            xws.CloseOutput = true;
            XmlWriter xw = XmlWriter.Create(pageFilename, xws);
            xw.WriteStartDocument();
            xw.WriteStartElement(PageFileXmlNames.RootElement);
            
            xw.WriteStartElement(PageFileXmlNames.PageNameElement);
            xw.WriteValue(pageName);
            xw.WriteEndElement();

            xw.WriteStartElement(PageFileXmlNames.PageNumberElement);
            xw.WriteValue(pageNumber);
            xw.WriteEndElement();

            xw.WriteStartElement(PageFileXmlNames.ThumbnailFileLocElement);
            xw.WriteValue(thumbnailFileLocation);
            xw.WriteEndElement();

            xw.WriteStartElement(PageFileXmlNames.XamlFileLocElement);
            xw.WriteValue(xamlFileLocation);
            xw.WriteEndElement();

            xw.WriteStartElement(PageFileXmlNames.ButtonMapFileLocElement);
            xw.WriteValue(buttonMapFileLocation);
            xw.WriteEndElement();

            xw.WriteStartElement(PageFileXmlNames.ButtonCounterElement);
            xw.WriteValue(buttonCounter);
            xw.WriteEndElement();
            
            xw.WriteEndElement();   //End Root Element
            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();
        }

        #endregion
    }
}
